package cn.com.headfree.sysmenu.service.impl;

import cn.com.headfree.entity.isc.User;
import cn.com.headfree.sysmenu.dao.IMenuDao;
import cn.com.headfree.sysmenu.dao.IRoleMenuDao;
import cn.com.headfree.sysmenu.entity.TSysMenu;
import cn.com.headfree.sysmenu.entity.TSysRoleMenu;
import cn.com.headfree.sysmenu.service.IMenuService;
import cn.com.headfree.sysmenu.vo.TreeSelect;
import com.gta.edu.sdk.base.service.impl.BaseServiceImpl;
import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.util.IdGen;
import com.gta.edu.sdk.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service("menuService")
public class MenuServiceImpl extends BaseServiceImpl<TSysMenu> implements IMenuService {
    @Autowired
    IMenuDao MenuDao;
    @Autowired
    IRoleMenuDao roleMenuDao;

    //查询菜单列表
    @Override
    public List<TSysMenu> findMenuList(TSysMenu sysMenu, User user) {
        List<TSysMenu> menuList = null;
        // 管理员显示所有菜单信息
       /* if (SysUser.isAdmin(userId)){
            menuList = MenuDao.findMenuList(sysMenu);
        }else{
            menuList = MenuDao.findMenuListByErpNo(sysMenu,user.getFax());
        }*/
        menuList = MenuDao.findMenuListByErpNo(sysMenu,user.getFax());
        return menuList;
    }

    //根据菜单名称查询菜单全部列表
    @Override
    public List<TSysMenu> findMenuListByMenuName(String MenuId, String MenuName) {
        return MenuDao.findMenuListByMenuName(MenuId, MenuName);
    }

    @Override
    public List<TreeSelect> buildMenuTreeSelect(List<TSysMenu> menus){
        List<TSysMenu> menuTrees = buildMenuTree(menus);
        return menuTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    /**
     * 构建前端所需要树结构
     *
     * @param menus 菜单列表
     * @return 树结构列表
     */
    @Override
    public List<TSysMenu> buildMenuTree(List<TSysMenu> menus){
        List<TSysMenu> returnList = new ArrayList<TSysMenu>();
        List<String> tempList = new ArrayList<String>();
        for (TSysMenu dept : menus)
        {
            tempList.add(dept.getId());
        }
        for (Iterator<TSysMenu> iterator = menus.iterator(); iterator.hasNext();)
        {
            TSysMenu menu = (TSysMenu) iterator.next();
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(menu.getParentId()))
            {
                recursionFn(menus, menu);
                returnList.add(menu);
            }
        }
        if (returnList.isEmpty())
        {
            returnList = menus;
        }
        return returnList;
    }

    /**
     * 递归列表
     *
     * @param list
     * @param t
     */
    private void recursionFn(List<TSysMenu> list, TSysMenu t)
    {
        // 得到子节点列表
        List<TSysMenu> childList = getChildList(list, t);
        t.setChildren(childList);
        for (TSysMenu tChild : childList)
        {
            if (hasChild(list, tChild))
            {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<TSysMenu> getChildList(List<TSysMenu> list, TSysMenu t)
    {
        List<TSysMenu> tlist = new ArrayList<TSysMenu>();
        Iterator<TSysMenu> it = list.iterator();
        while (it.hasNext())
        {
            TSysMenu n = (TSysMenu) it.next();
            if (null!=n.getParentId() && (n.getParentId().equals(t.getId())))
            {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<TSysMenu> list, TSysMenu t)
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }

    //根据角色ID查询菜单树id集合
    @Override
    public List<String> selectMenuIdListByRoleId(String roleId){
        List<TSysMenu> list = MenuDao.selectMenuListByRoleId(roleId);
        return list.stream().map(TSysMenu::getId).collect(Collectors.toList());
    }

    //根据角色ID查询菜单树信息
    @Override
    public List<TSysMenu> selectMenuListByRoleId(String roleId){
        return MenuDao.selectMenuListByRoleId(roleId);
    }

    @Override
    public boolean hasChildByMenuId(String menuId){
        List<TSysMenu> list = MenuDao.selectMenuListByParentId(menuId);
        return list.size() > 0 ? true : false;
    }

    @Override
    public boolean checkMenuExistRole(String menuId){
        List<TSysRoleMenu> list = roleMenuDao.checkMenuExistRole(menuId);
        return list.size() > 0 ? true : false;
    }

    //新增菜单
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void addMenu(TSysMenu sysMenu){
        String id = IdGen.uuid();
        sysMenu.setId(id);
        TSysMenu parentMenu = this.get(TSysMenu.class,sysMenu.getParentId());
        String pathId = id;
        String pathName = String.format("/%s",sysMenu.getMenuName());
        if(parentMenu!=null){
            pathId =  String.format("%s/%s",parentMenu.getPathId(),pathId);
            pathName = String.format("%s%s",parentMenu.getPathName(),pathName);
        }else{
            sysMenu.setParentId("0");
        }
        sysMenu.setPathId(pathId);
        sysMenu.setPathName(pathName);
        MenuDao.addMenu(sysMenu);
    }

    //新增菜单
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateMenu(TSysMenu sysMenu){
        TSysMenu parentMenu = this.get(TSysMenu.class,sysMenu.getParentId());
        String pathId = sysMenu.getId();
        String pathName = String.format("/%s",sysMenu.getMenuName());
        if(parentMenu!=null){
            pathId =  String.format("%s/%s",parentMenu.getPathId(),pathId);
            pathName = String.format("%s%s",parentMenu.getPathName(),pathName);
        }else{
            sysMenu.setParentId("0");
        }
        sysMenu.setPathId(pathId);
        sysMenu.setPathName(pathName);
        MenuDao.updateMenu(sysMenu);
    }

    //批量删除菜单
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteMenu(String[] MenuIds){
        MenuDao.deleteMenu(MenuIds);
    }

    //删除菜单
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteMenuById(String menuId){
        MenuDao.deleteMenuById(menuId);
    }
}
